<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.lang.*,java.sql.*,javax.naming.*,javax.sql.*"%>
<%@ page import="service.*,java.math.BigDecimal,db.*;"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	Connection con = null;
	Statement stmt = null;
	ResultSet rs = null;

String adminId  =  request.getParameter("adminId");
int n = Integer.parseInt(adminId);
if(n>0){
	try {
		con = DBConn.getConn();
		stmt = con.createStatement();
		String sql = "select * from shop_admin ,shop_admingroup where shop_admin.admin_groupId = shop_admingroup.admin_group_id and shop_admin.admin_id = ' "+adminId+"'";
		rs = stmt.executeQuery(sql);
	} catch (SQLException e) {
		System.out.println(e.toString());
	}
}
%>

<!DOCTYPE HTML>
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP</title>
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	
	<style type="text/css">
body{
	width:200px;
	margin:auto;
}
td{
	margin-bottom: 20px;
}
.inputBox{
	height:20px;
	width:120px;
}
</style>

<script type="text/javascript">
$(document).ready(function(){
	$("#adminType").val($("#adminType").attr("ref"));
	$("#creatBt").click(function(){
		 if(!check()){
			return;
		} 
		var  userName = $("input#userName").val();
		var  passWord = $("input#passWord").val();
		var  mailBox = $("input#mailBox").val();
		var  phoneNum = $("input#phoneNum").val();
		var  adminType = $("select#adminType").val();
		$.ajax({
			type : "POST",
			url : "servlet/ManageAdminServlet",
			data: "action=add&userName="+userName+"&passWord="+passWord+"&mailBox="+mailBox+"&phoneNum="+phoneNum+"&adminType="+adminType,
			success : function(result) {
				if(result=="success"){
					window.parent.location.reload(true);
				}else if(result=="nameAlreadyHave"){
					alert("您输入的用户名已被注册，请重新输入");
				}else if(result=="mailBoxAlreadyHave"){
					alert("您输入的邮箱已被注册，请重新输入");
				}else if(result=="phoneAlreadyHave"){
					alert("您输入的电话号码已被注册，请重新输入");
				}
			}
		});	
	});
	
	$("#changeBt").click(function(){
		 if(!check()){
			return;
		} 
		var  userName = $("input#userName").val();
		var  passWord = $("input#passWord").val();
		var  mailBox = $("input#mailBox").val();
		var  phoneNum = $("input#phoneNum").val();
		var  adminType = $("select#adminType").val();
		$.ajax({
			type : "POST",
			url : "servlet/ManageAdminServlet",
			data: "action=change&userName="+userName+"&passWord="+passWord+"&mailBox="+mailBox+"&phoneNum="+phoneNum+"&adminType="+adminType+"&adminId="+<%=adminId%>,
			success : function(result) {
				if(result=="success"){
					window.parent.location.reload(true);
				}else if(result=="nameAlreadyHave"){
					alert("您输入的用户名已被注册，请重新输入");
				}else if(result=="mailBoxAlreadyHave"){
					alert("您输入的邮箱已被注册，请重新输入");
				}else if(result=="phoneAlreadyHave"){
					alert("您输入的电话号码已被注册，请重新输入");
				}
			}
		});	
	});
	
	$("#reset").click(function(){
		$("#userName").val("");
		$("#passWord").val("");
	});
	
	$("#cancelBt").click(function(){
		window.parent.location.reload(true);
		//window.location.reload(true);
	});
	
});


 function check()
 {
     if ($("#userName").val()=="")
     {
        alert("请输入用户名!!!");
 		$("#userName").focus(); 
       return false;
     }
     if ($("#passWord").val()=="")
     {
       alert("请输入密码!!!");
       $("#passWord").focus(); 
       return false;
     }
     if($("#passWord").val()!=$("#rePassWord").val())
     {
     	alert("您两次输入的密码不相同");
     	$("#rePassWord").focus(); 
     	return false;
     }
     if (!(/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test($("#mailBox").val()))) {
    	alert("您输入的邮箱格式不对！！！");
        return false;
     }
     if (!(/^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|14[0-9]|15[0|2|3|6|7|8|9]|18[3|6|7|8|9])\d{8}$)/.test($("#phoneNum").val()))) {
		alert("电话号码格式不对");
		return false;
    }
     return true;
 }
 </script>

  </head>
  
  <body>
    <table style="margin-top:10px;">
    <%if(n==0) {%>
    	<tr>
        	<td height="18px">用户名</td>
            <td><input class="inputBox" id="userName" type="text" value="" name="userName" /></td>
        </tr>
        <tr>
        	<td height="18px" >密码</td>
            <td><input class="inputBox" id="passWord" type="password" name="passWord" /></td>
        </tr>
        <tr>
        	<td height="18px">确认密码</td>
            <td><input class="inputBox" id="rePassWord" type="password" name="rePassWord" /></td>
        </tr>
        <tr>
        	<td height="18px">邮箱</td>
            <td><input class="inputBox" id="mailBox" type="text" name="mailBox" /></td>
        </tr>
        <tr>
        	<td height="18px">联系电话</td>
            <td><input class="inputBox" id="phoneNum" type="text" name="phoneNum" /></td>
        </tr>
        <tr>
        	<td height="18px">管理员类型</td>
            <td><select name="adminType" id="adminType"> 
            	<option selected="selected" value="1" >客商管理员</option>         			
            	<option value="2">物流统计管理员</option>         
            	<option value="3">财务管理员</option>         
                </select> 
            </td>
        </tr>
        <tr>
        	<td><input type="button" id="creatBt" value="添加" name="subButton"/></td>
        	<%}else if(n>0) { while(rs.next()){%>
        	
        	<tr>
        	<td height="18px">用户名</td>
            <td><input class="inputBox" id="userName" type="text" value="<%=rs.getString("admin_name")%>" name="userName" /></td>
        </tr>
        <tr>
        	<td height="18px" >密码</td>
            <td><input class="inputBox" id="passWord" type="password" name="passWord" /></td>
        </tr>
        <tr>
        	<td height="18px">确认密码</td>
            <td><input class="inputBox" id="rePassWord" type="password" name="rePassWord" /></td>
        </tr>
        <tr>
        	<td height="18px">邮箱</td>
            <td><input class="inputBox" id="mailBox" value="<%=rs.getString("admin_email")%>" type="text" name="mailBox" /></td>
        </tr>
        <tr>
        	<td height="18px">联系电话</td>
            <td><input class="inputBox" id="phoneNum" value="<%=rs.getString("admin_tel")%>" type="text" name="phoneNum" /></td>
        </tr>
        <tr>
        	<td height="18px">管理员类型</td>
            <td><select name="adminType" id="adminType" ref="<%=rs.getString("admin_group_id")%>"> 
            	<option selected="selected" value="1" >客商管理员</option>         			
            	<option value="2">物流统计管理员</option>         
            	<option value="3">财务管理员</option>         
                </select> 
            </td>
        </tr>
        <tr>
        	<td><input type="button" id="changeBt" value="修改" name="chaButton"/></td>   
        	<%
        	
        	} 
        	rs.close();
        	con.close();
        	}

        	%>
        	<td><input type="button" id="cancelBt" value="取消" name="resButton"/></td>
        </tr>
    </table>
    
  </body>
</html>
